package core;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import model.Category;
import model.Checkin;
import model.Item;
import model.Member;
import model.Photo;

import org.jboss.annotation.ejb.LocalBinding;

import utils.Utils;

@Stateless
@LocalBinding(jndiBinding="local/Manager")
public class Manager implements ManagerLocal{

    @PersistenceContext
    private EntityManager em;
    
    public Object persist(Object obj){
    	em.persist(obj);
    	return obj;
    }
    
    public Object merge(Object obj){
    	em.merge(obj);
    	return obj;
    }    

	@Override
	public <T> T get(Class<T> _class, Integer id) {
		return em.find(_class, id);
	}

	@Override
	public <T> void remove(Class<T> _class, Integer id) {
		em.remove(get(_class, id));
	}

	@Override
	public List<Category> listCategory(Boolean leaf, Integer idCategory) {
		StringBuilder sb = new StringBuilder();
		sb.append("select distinct c ");
		sb.append("from Category c ");
		
		
		if(idCategory != null){
			 sb.append("where c.parent.idCategory = :idCategory ");
		}else if(leaf != null && leaf){
			sb.append("where not exists (select c2 from Category c2 where c2.parent.idCategory = c.idCategory) ");
		}else {
			 sb.append("where c.parent is null ");
		}

		sb.append("order by c.name");
		
		Query q = em.createQuery(sb.toString());
		
		if(idCategory != null){
			q.setParameter("idCategory", idCategory);
		}
		
		return q.getResultList();
	}

	@Override
	public List<Item> listItems(Member member, Integer idCategory) {
		StringBuilder sb = new StringBuilder();
		sb.append("select distinct i ");
		sb.append("from Item i join fetch i.categories c ");
		
		if(idCategory != null && member.isSuperAdmin()){
			 sb.append("where c.idCategory = :idCategory ");
		}else if(idCategory != null && !member.isSuperAdmin()){
			sb.append("where c.idCategory = :idCategory and i.member.idMember = :idMember ");
		}else if(idCategory == null && !member.isSuperAdmin()){
			sb.append("where i.member.idMember = :idMember ");
		}
		
		Query q = em.createQuery(sb.toString());
		
		if(idCategory != null){
			q.setParameter("idCategory", idCategory);
		}
		
		if(!member.isSuperAdmin()){
			q.setParameter("idMember", member.getIdMember());
		}
		
		return q.getResultList();
	}

	@Override
	public List<Photo> listPhotos(Integer idItem) {
		return em.createQuery("select p from Photo p where p.item.idItem = :idItem order by p.path").setParameter("idItem", idItem).getResultList();
	}

	@Override
	public List<Checkin> getCheckinsItems(Integer idItem,
			String memberCellphone, String memberEmail, String memberPassword) {

		StringBuilder sb = new StringBuilder();
		sb.append("select distinct c ");
		sb.append("from Checkin c join fetch c.item i join fetch c.member m ");
		
		if(idItem != null && !Utils.isEmpty(memberCellphone)){
			 sb.append("where i.idItem = :idItem and m.cellphone = :cellphone ");
		}else if(idItem != null && !Utils.isEmpty(memberEmail)){
			sb.append("where i.idItem = :idItem and m.email = :email ");
		}else if(idItem != null && Utils.isEmpty(memberEmail) && Utils.isEmpty(memberCellphone)){
			sb.append("where i.idItem = :idItem ");
		}else if(idItem == null && !Utils.isEmpty(memberCellphone)){
			sb.append("where m.cellphone = :cellphone ");
		}else if(idItem == null && !Utils.isEmpty(memberEmail)){
			sb.append("where m.email = :email ");
		}
		
		sb.append("order by c.datetime desc");
		
		Query q = em.createQuery(sb.toString());
		
		if(idItem != null){
			q.setParameter("idItem", idItem);
		}
		
		if(!Utils.isEmpty(memberCellphone)){
			q.setParameter("cellphone", memberCellphone);
		}else if(!Utils.isEmpty(memberEmail)){
			q.setParameter("email", memberEmail);
		}
		
		return q.getResultList();		
	}
    
    
}
